import 'package:detect/utils/http_util.dart';

class ApiUtil {
  final HttpUtil http;

  ApiUtil({
    // String baseUrl = "http://192.168.1.123:8080",
    String baseUrl = "http://192.168.1.2:8080",
    Map<String, String> headers = const {'content-type': 'application/x-www-form-urlencoded'},
  }) : http = HttpUtil(baseUrl: baseUrl, headers: headers);

  Future<Map<String, dynamic>> get fpgaState async => (await http.get("/api/fpga/state"))['data'];
  
  Future<void> listenStatus() => http.post("/api/fpga/state/start");

  Future<void> reset() => http.post("/api/fpga/reset");

  Future<void> write(int addr, int value) => http.post("/api/fpga/value", {"addr": addr, "value": value});

  Future<String> read(int addr) async => (await http.get("/api/fpga/value", {"addr": addr}))['data']['value'];

  Future<void> decay(int value) => http.post("/api/fpga/decay", {"value": value});

  Future<void> sendText(String msg) => http.post("/api/work/text", {"msg": msg});

  Future<void> sendLoraData(Map<String, dynamic> params) => http.post("/api/fpga/lora", params);

  Future<void> sendStandardWave(Map<String, dynamic> params) => http.post("/api/fpga/wave", params);

  Future<void> setLoraReceiveFreq(int freq) => http.post("/api/fpga/lora/freq", {"freq": freq});

  Future<void> spectrum(Map<String, dynamic> params) => http.post("/api/fpga/pp", params);

  Future<void> setFreqs(String type, [String? freqs]) => http.post("/api/detect/freqs", {"type": type, "freqs": freqs ?? ''});

  Future<void> startDetect(String type, String role, int remoteId) => http.post("/api/detect/start", {"type": type, "role": role, "remote_id": remoteId});

  Future<void> stopDetect() => http.post("/api/detect/stop");
}
